package com.uchengwang.functions.jpush;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.cm_ui.umsg.bean.SchoolClubBean;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;


/**
 * Created by U城网 on 2017/6/25.
 * 极光设置别名 tag
 */

public class JpushSetAlisTagUtil {
    private Context context;
    private final int MSG_SET_ALIAS = 1001;
    private final int MSG_SET_TAG = 1002;

    private static  JpushSetAlisTagUtil util;
    private JpushSetAlisTagUtil (){
    }

    public static JpushSetAlisTagUtil getUtil() {
        if (util == null){
            util = new JpushSetAlisTagUtil();
        }
        return util;
    }

    /**
     * 极光 设置别名
     * */
    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs ;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success";
                    Log.d("jg", logs);
                    // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    Log.d("jg", logs);
                    // 延迟 60 秒来调用 Handler 设置别名
                    if (!TextUtils.isEmpty(alias)){
                        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 50);
                    }else {
                        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_TAG, tags), 1000 * 50);
                    }

                    break;
                default:
                    logs = "Failed with errorCode = " + code;
                    Log.d("jg", logs);
            }
        }
    };

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_SET_ALIAS:
                    Log.d("jg", "Set alias in handler.");
                    // 调用 JPush 接口来设置别名。
                    JPushInterface.setAliasAndTags(context, String.valueOf(msg.obj), null, mAliasCallback);
                    break;
                case MSG_SET_TAG:
                    JPushInterface.setAliasAndTags(context, null, (Set<String>) msg.obj, mAliasCallback);
                    break;
                default:
                    Log.d("jg", "Unhandled msg - " + msg.what);
            }
        }
    };

    /**
     * 设置别名
     * */
    public void setAlis(Context context){
        this.context = context.getApplicationContext();
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, UCommonUtil.getUtil().getId()));
    }
    /**
     * 设置空别名 不登录不收到极光推送
     * */
    public void setNullAlis(Context context){
        this.context = context.getApplicationContext();
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, 0));
    }

    /**
     * @param tags 设置tag
     * */
    public void setTags(Context context, Set<String> tags){
        this.context = context.getApplicationContext();
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_TAG, tags));
    }

    /**
     * 设置社团的tag
     * */
    public void setClubTags(final Activity activity){
        if (UCommonUtil.getUtil().isLogin()){
            if (UChengApplication.userRole == BaseConfig.CLUB_ROLE){
                //社团账号
                Set<String> stringSet = new HashSet<String>();//空数组或列表表示取消之前的设置。
                String tag = "org_" + UCommonUtil.getUtil().getId();
                stringSet.add(tag);
                setTags(activity, stringSet);
            }else {
                HttpMeManager.getPresenter().getMyClub(activity, true, 1, 999, new OnHttpResultListener<SchoolClubBean>() {
                    @Override
                    public void onResult(int status, String msg, int total, final List<SchoolClubBean> listCreate) {
                        getMyJoinClubs(activity, listCreate);
                    }
                });
            }
        }else {
            Set<String> stringSet = new HashSet<String>();//空数组或列表表示取消之前的设置。
            setTags(activity, stringSet);
        }
    }

    /**
     * 获取自己加入的社团
     * */
    private void getMyJoinClubs(final Activity activity, final List<SchoolClubBean> listCreate){
        //调用自己加入的社团
        HttpMeManager.getPresenter().getMyClub(activity, false, 1, 999, new OnHttpResultListener<SchoolClubBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<SchoolClubBean> listJoin) {
                Set<String> stringSet = new HashSet<String>();
                if (listCreate != null && listCreate.size() >0){
                    for (SchoolClubBean bean :listCreate){
                        String tag = "org_" + bean.getUid();
                        stringSet.add(tag);
                    }
                }

                if (listJoin != null && listJoin.size() >0){
                    for (SchoolClubBean bean :listJoin){
                        String tag = "org_" + bean.getUid()+"_member_"+ UCommonUtil.getUtil().getId();
                        stringSet.add(tag);
                    }
                }

                setTags(activity, stringSet);
            }
        });
    }




}
